<template>
  <el-dialog title="新增员工" width="600px" :visible.sync="show">
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-input v-model="form.timeOfEntry" />
      </el-form-item>
      <el-form-item label="聘用形势" prop="formOfEmployment">
        <!-- 必要的传参 -->
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <div >
          <el-input
            v-model="form.departmentName"
            readonly
            @focus="focusEvent"
          />
          <el-tree
            v-if="treeShow"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-input v-model="form.correctionTime" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align:center">
        <el-button type="primary">提交</el-button>
        <el-button>取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employeesData from '@/api/constant/employees'
import { department } from '@/api/treeitem.js'

export default {
  data () {
    return {
      show: false,
      employeesData,
      treeShow: false,
      treeData: [],
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              validatePhone(value)
                ? callback()
                : callback(new Error('请正确输入手机号'))
            }
          }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    async focusEvent () {
      this.treeShow = true
      if (this.treeData.length === 0) {
        const res = await department()
        this.treeData = this.changeDate(res.data.depts, '')
        console.log('组织架构', res)
      }
    },
    nodeClick (data) {
      this.treeData = false
      this.form.departmentName = data.name
    },
    changeDate (arr, pid = '') {
      return arr.filter(item => {
        if (item.pid === pid) {
          item.children = this.changeDate(arr, item.id)
          return true
        }
      })
    },
    treeCloseEvent () {
      this.treeShow = false
    },
    // submit () {
    //   this.$refs.form.validate(async result => {
    //     if (result) {
    //       const res = await sysUserPost(this.form)
    //       console.log(res)
    //     }
    //   })
    // }
  }
}
</script>